<?php
function listFiles($folder, $index=false, $type = 'files', $validExtensions = null)
{
	if ($ponteiro  = openFolder($folder) )
    {
        while ( $item = readdir($ponteiro) )
        {
            $itens[] = $item;
        }

        sort($itens);

        foreach ($itens as $line => $info)
        {
            if ($info!="." && $info!="..")
            {
                if ( is_dir($info) )// if is directory
                {
                    if ($index)
                    {
                        $folders[$info]  = $info;
                    }
                    else
                    {
                        $folders[]  = $info;
                    }
                }
                else // if is file
                {
                    $ok = true; //temp variable

                	if ( $validExtensions )
                	{
                        $split = explode('.', $info);
                        $extension = $split[ count( $split )-1 ];

                        if ( !in_array( $extension,  $validExtensions ) )
                        {
                            $ok = false;
                        }
                	}

                	if ( $ok )
                	{
	                    if ( $index )
	                    {

	                        $files[$info]    = $info;
	                    }
	                    else
	                    {
	                        $files[]    = $info;
	                    }
                	}
                }
            }
        }
        if ($type == 'files')
        {
            return $files;
        }
        else
        {
        	return $folders;
        }
    }
}

function listImages($folder, $validExtensions = null )
{
	//default valid extensions
	if ( !$validExtensions )
	{
		$validExtensions[] = 'jpg';
		$validExtensions[] = 'jpeg';
		$validExtensions[] = 'png';
		$validExtensions[] = 'gif';
		$validExtensions[] = 'bmp';
	}

	$files = listFiles($folder, false, 'files' , $validExtensions);

	return $files;
}

/**
* Function the open a Folder forcint it to be created if it does not exists.
*
*/
function openFolder($folder)
{
    if (is_dir($folder) )
    {
        return opendir($folder);
    }
    else
    {
        if (mkdir($folder, 0777))
        {
            return opendir($folder);
        }
        else
        {
            return false;
        }
    }
}

/**
* Make a upload of a file
*
*/
function uploadFile($target_path, $parseFilename=NULL)
{
	if ($_FILES)
	{
		$filename = basename( $_FILES['fileUpload']['name']);
		if ($parseFilename)
		{
			$filename = str_replace(' ', '_', $filename);
		}
		$target_path = $target_path . $filename;

		if(move_uploaded_file($_FILES['fileUpload']['tmp_name'], $target_path))
		{
	    	return basename( $filename);
		}
		else
		{
	    	return "false";
		}
	}
	else
	{
		return null;
	}
}

function deleteFile($folder, $file)
{
	//verificar se arquivo existe ou n�o...
	return unlink($folder.$file);
}

?>